package com.young4j.xinsuiwutuobang.bean;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/cookie")
public class CookieTest extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//获取Cookie
		Cookie[] cookies = req.getCookies();
		for(Cookie cookie : cookies){
			System.out.println(cookie.getName() + ":" + cookie.getValue());
		}
		//创建Cookie
		Cookie cookie = new Cookie("cookieTestName", "cookieTestValue");
		/**
		 * Sets the maximum age in seconds for this Cookie. A positive value indicates that 
		 * the cookie will expire after that many seconds have passed. Note that the value 
		 * is the maximum age when the cookie will expire, not the cookie's current age. 
		 * A negative value means that the cookie is not stored persistently 
		 * and will be deleted when the Web browser exits. A zero value causes 
		 * the cookie to be deleted.
		 * 正值标识Cookie多少秒之后过期，0标识Cookie被删除，负数表示Cookie不会永久存储，浏览器退出时将被删除
		 */
		cookie.setMaxAge(10);
		cookie.setHttpOnly(true);
		resp.addCookie(cookie);
		PrintWriter pw = resp.getWriter();
		pw.print("<html><body><h1>cookie test!</h1></body></html>");
	}
}
